package com.fatmeraid.controller.product;

import com.fatmeraid.common.model.PageResult;
import com.fatmeraid.common.result.Result;
import com.fatmeraid.domain.Product;
import com.fatmeraid.service.ProductService;
import com.fatmeraid.vo.PaginationVo;
import com.fatmeraid.vo.product.ReviewProductVo;
import com.fatmeraid.vo.product.UploadVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商品管理管理")
@RestController
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private ProductService productService;
	@Operation(summary = "上传商品")
	@PostMapping
	public Result<String> uploadProduct(@RequestBody UploadVo uploadVo) {
		String result = String.valueOf(productService.uploadProduct(uploadVo));
		return Result.ok(result);
	}

	@Operation(summary = "获取商品列表")
	@GetMapping
	public Result<PageResult<Product>> getProductList(PaginationVo paginationVo) {
		PageResult<Product> list = productService.getProductList(paginationVo);
		return Result.ok(list);
	}

	@Operation(summary = "根据商品id获取商品详情")
	@GetMapping("/getProductById/{id}")
	public Result<Product> getProductById(@PathVariable Long id) {
		Product product = productService.getProductById(id);
		return Result.ok(product);
	}

	@Operation(summary = "审核商品")
	@PutMapping
	public Result<Product> auditProduct(@RequestBody ReviewProductVo reviewProductVo) {
		boolean result = productService.auditProduct(reviewProductVo);
		if (!result) {
			return Result.fail();
		}
		return Result.ok();
	}

	@Operation(summary = "删除商品")
	@DeleteMapping("/{id}")
	public Result<Product> deleteProduct(@PathVariable Long id) {
		boolean result = productService.deleteProduct(id);
		if (!result) {
			return Result.fail();
		}
		return Result.ok();
	}

	// 获取所有已经上架的商品
	@Operation(summary = "获取所有已经上架的商品")
	@GetMapping("/getAllOnSale")
	public Result<PageResult<Product>> getAllOnSale(PaginationVo paginationVo) {
		PageResult<Product> list = productService.getAllOnSale(paginationVo);
		return Result.ok(list);
	}


}
